package com.hankol.newretail.adapter.details

import android.annotation.SuppressLint
import android.app.Activity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.baselibrary.extension.checkValue
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.utils.GlideUtils
import com.hankol.newretail.R
import com.hankol.newretail.bean.goods_details.GoodsDetailsBanner
import com.hankol.newretail.utils.RetailPopUtils
import com.hankol.newretail.widget.GoodsVideoView
import com.tencent.rtmp.TXVodPlayer

/**
 * @author: 关忆北、
 * @date: 2022/8/2
 * @Des：商品详情封面
 */
class GoodsCoverVpAdapter(var activity: Activity, var commonVodPlayer: TXVodPlayer) : PagerAdapter() {

    private var list: ArrayList<GoodsDetailsBanner> = arrayListOf()
    private var goodName =""
    fun setData(list: ArrayList<GoodsDetailsBanner>) {
        this.list = list
        notifyDataSetChanged()
    }

    fun setGoodName(goodName: String) {
        this.goodName = goodName
    }

    var goodsVideoView: GoodsVideoView? = null

    override fun getCount(): Int {
        return list.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, view: Any) {
        container.removeView(view as View)
    }

    @SuppressLint("SetTextI18n")
    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val inflate = LayoutInflater.from(container.context).inflate(R.layout.item_goods_details_media, container, false)
        val ivGoodsMedialCover = inflate.findViewById<ImageView>(R.id.iv_goods_medial_cover)
        val mGsvGoodsMedialVideo = inflate.findViewById<GoodsVideoView>(R.id.gsv_goods_medial_video)
        if (list[position].coverType == GoodsDetailsBanner.GOODS_BANNER_VIDEO) {
            goodsVideoView = mGsvGoodsMedialVideo
            ivGoodsMedialCover.visibility = View.GONE
            mGsvGoodsMedialVideo.visibility = View.VISIBLE
            mGsvGoodsMedialVideo.mVodPlayer = commonVodPlayer
            mGsvGoodsMedialVideo.setVideoUrl(list[position].video, list[position].cover)
            mGsvGoodsMedialVideo.setPlayMode(1)
        } else {
            ivGoodsMedialCover.visibility = View.VISIBLE
            mGsvGoodsMedialVideo.visibility = View.GONE
            GlideUtils.loadImgNoCache(activity.applicationContext, list[position].cover, ivGoodsMedialCover)
        }

        inflate.setOnClickListener {
            SensorsDataManager.instance.productCarousel(if (list[position].coverType == GoodsDetailsBanner.GOODS_BANNER_IMG) "图片" else "视频", goodName.checkValue())
            if (mGsvGoodsMedialVideo.isPlay || list[position].coverType == GoodsDetailsBanner.GOODS_BANNER_IMG) {
                mGsvGoodsMedialVideo.stopPlay()
                val goodsCoverPopVpAdapter = GoodsCoverPopVpAdapter(activity, list, mGsvGoodsMedialVideo, position)
                RetailPopUtils.goodsCoverPop(activity, goodsCoverPopVpAdapter, position, object : GoodsCoverPopVpAdapter.OnItemClickListener {
                    override fun onItemClick(gsvGoodsMedialVideo: GoodsVideoView?) {
                        mGsvGoodsMedialVideo.mVodPlayer = commonVodPlayer
                        mGsvGoodsMedialVideo?.vodPlayer?.setPlayerView(mGsvGoodsMedialVideo.mTxclGoodsDetVideo)
                        mGsvGoodsMedialVideo?.isVoice?.let { mGsvGoodsMedialVideo.isVoice(it) }
                        mGsvGoodsMedialVideo?.vodPlayer?.resume()
                        mGsvGoodsMedialVideo.visibilityCover()
                        goodsVideoView = mGsvGoodsMedialVideo
                    }

                })
            }
        }
        container.addView(inflate)
        return inflate
    }

}